main = print (largestfactor 600851475143)

largestfactor :: Int -> [Int]
largestfactor x = reverse (filter (factorOf x) (takeWhile (<(ceiling (sqrt (fromIntegral x)))) primes))

factorOf :: Int -> Int -> Bool
factorOf x y = x `rem` y == 0

primes :: [Int]
primes = 2: 3: sieve (tail primes) [5,7..]
 where 
  sieve (p:ps) xs = h ++ sieve ps [x | x <- t, x `rem` p /= 0]
                  where (h,~(_:t)) = span (< p*p) xs
